package win.smartown.aqst.demo.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;

import win.smartown.aqst.demo.R;

/**
 * @author 雷小武
 * 创建时间：2018/11/7 14:41
 * 版权：成都智慧一生约科技有限公司
 * 类描述：
 */
public class RoundButton extends AppCompatTextView {

    private int color;
    private int colorPressed;
    private int textColor;
    private int textColorPressed;

    private Paint paint;
    private RectF rectF;

    public RoundButton(Context context) {
        super(context);
        init(null);
    }

    public RoundButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public RoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = getResources().obtainAttributes(attrs, R.styleable.RoundButton);
            color = typedArray.getColor(R.styleable.RoundButton_color, Color.DKGRAY);
            colorPressed = typedArray.getColor(R.styleable.RoundButton_colorPressed, Color.LTGRAY);
            textColor = typedArray.getColor(R.styleable.RoundButton_textColor, Color.WHITE);
            textColorPressed = typedArray.getColor(R.styleable.RoundButton_textColorPressed, Color.WHITE);
            typedArray.recycle();
        } else {
            color = Color.DKGRAY;
            colorPressed = Color.LTGRAY;
            textColor = Color.WHITE;
            textColorPressed = Color.WHITE;
        }
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        setTextColor(textColor);
        setBackgroundDrawable(null);
        setGravity(Gravity.CENTER);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                paint.setColor(colorPressed);
                setTextColor(textColorPressed);
                invalidate();
                return true;
            case MotionEvent.ACTION_MOVE:
                paint.setColor(colorPressed);
                setTextColor(textColorPressed);
                invalidate();
                break;
            default:
                paint.setColor(color);
                setTextColor(textColor);
                invalidate();
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (rectF == null) {
            rectF = new RectF(0, 0, getWidth(), getHeight());
        }
        int radius = getHeight() / 2;
        canvas.drawRoundRect(rectF, radius, radius, paint);
        super.onDraw(canvas);
    }
}